<?php 

require_once('../include/GeneralTop.php');
require('../include/class/db.php');
require('../include/class/pagination.php');
require('../include/simpleHeader.php');

$loginFormAction = $_SERVER['PHP_SELF'];

/*
if (!isset($_SESSION['Index_Username'])) {
	die('<div style="text-align:center">'.MSG_PERMISSION_DENIED.'</div>');
}
*/
?>

<body topmargin="0" leftmargin="0">
<table width="777" border="0" align="left" cellpadding="0" cellspacing="0" valign="top">
  <tr>
    <td width="777" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="51" valign="top" background="../images/fen_09.gif"><table width="95%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="16">&nbsp;</td>
            </tr>
          </table>
          <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td width="14"><img src="../images/fen_12.gif" width="7" height="29"></td>
              <td width="405"><span class="font_16px_black_B"><?php echo TEXT_SOS_CAT?></span></td>
              <td width="15">&nbsp;</td>
              <td width="299">&nbsp;</td>
              </tr>
          </table></td>
        </tr>
      </table>
      <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td height="1" bgcolor="#767676"></td>
        </tr>
      </table>
      <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td height="8"></td>
        </tr>
      </table>
      <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" class="font_12px_gray">
        <tr>
          <td valign="top" class="font_12px_gray">
          <table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="table_border_grey">
  <tr>
    <td colspan="12">
	  <form action="<?php echo $_SERVER['PHP_SELF']?>" method="GET">
	  <?php echo TEXT_SEARCH_CAT?>： 
	  <?php
	  if($_GET['age'] == '10-99') {
	  	  $select1 = '';
	  	  $select2 = '';
	  	  $select3 = '';
	  	  $select4 = '';
	  	  $select5 = '';
	  	  $select6 = 'selected';
	  }
	  else if($_GET['age'] == '0-1') {
	  	  $select1 = '';
	  	  $select2 = 'selected';
	  	  $select3 = '';
	  	  $select4 = '';
	  	  $select5 = '';
	  	  $select6 = '';
	  }
	  else if($_GET['age'] == '1-3') {
	  	  $select1 = '';
	  	  $select2 = '';
	  	  $select3 = 'selected';
	  	  $select4 = '';
	  	  $select5 = '';
	  	  $select6 = '';
	  }
	  else if($_GET['age'] == '4-6') {
	  	  $select1 = '';
	  	  $select2 = '';
	  	  $select3 = '';
	  	  $select4 = 'selected';
	  	  $select5 = '';
	  	  $select6 = '';
	  }
	  else if($_GET['age'] == '7-9') {
	  	  $select1 = '';
	  	  $select2 = '';
	  	  $select3 = '';
	  	  $select4 = '';
	  	  $select5 = 'selected';
	  	  $select6 = '';
	  }
	  else {
	  	  $select1 = 'selected';
	  	  $select2 = '';
	  	  $select3 = '';
	  	  $select4 = '';
	  	  $select5 = '';
	  	  $select6 = '';
	  }
	  ?>
		&nbsp;&nbsp;<?php echo TEXT_AGE?> <select name="age" class="font_12px_gray_h18">
		  	  <option value="all" <?php echo $select1?>><?php echo TEXT_ALL?></option>
		  	  <option value="0-1" <?php echo $select2?>>0-1岁</option>
		  	  <option value="1-3" <?php echo $select3?>>1-3岁</option>
			  <option value="4-6" <?php echo $select4?>>4-6岁</option>
			  <option value="7-9" <?php echo $select5?>>7-9岁</option>
			  <option value="10-99" <?php echo $select6?>>10岁以上</option>
			</select>
		<!--
		&nbsp;&nbsp;&nbsp;&nbsp;<?php echo TEXT_VETERINARY_DISTRICT?> 
		  <select name="area" id="area" class="font_12px_gray_h18">
	        <option value="all"><?php echo TEXT_ALL?></option>
	        <?php
	        foreach($district_arr as $_key => $_value) {
	        	if($_GET['area'] == $_value) {
	        		echo '<option value="'.$_value.'" selected>'.$_value.'</option>';
	        	}
	        	else {
	        		echo '<option value="'.$_value.'">'.$_value.'</option>';
	        	}
	        }        
	        ?>
	      </select>
	      -->
		<?php
		if((int)$_GET['sex'] === 1) {
			$select1 = '';
	  	    $select2 = 'selected';
	  	    $select3 = '';
		}
		else if((int)$_GET['sex'] === 2) {
			$select1 = '';
	  	    $select2 = '';
	  	    $select3 = 'selected';
		}
		else {
			$select1 = 'selected';
	  	    $select2 = '';
	  	    $select3 = '';
		}
		?>
		&nbsp;&nbsp;&nbsp;&nbsp;
		<?php echo TEXT_GENDER?> <select name="sex" class="font_12px_gray_h18">
		  	  <option value="all" <?php echo $select1?>><?php echo TEXT_ALL?></option>
		  	  <option value="1" <?php echo $select2?>>Boy</option>
		  	  <option value="2" <?php echo $select3?>>Girl</option>
			</select>
		<?php
		if($_GET['spay'] == 'Y') {
			$select1 = '';
	  	    $select2 = 'selected';
	  	    $select3 = '';
		}
		else if($_GET['spay'] == 'N') {
			$select1 = '';
	  	    $select2 = '';
	  	    $select3 = 'selected';
		}
		else {
			$select1 = 'selected';
	  	    $select2 = '';
	  	    $select3 = '';
		}
		?>
		&nbsp;&nbsp;&nbsp;&nbsp;
		是否绝育 <select name="spay" class="font_12px_gray_h18">
		  	  <option value="all" <?php echo $select1?>><?php echo TEXT_ALL?></option>
		  	  <option value="Y" <?php echo $select2?>>已绝育</option>
		  	  <option value="N" <?php echo $select3?>>未绝育</option>
			</select>
	    &nbsp;&nbsp;<input type="submit" name="submit" id="submit" value=" <?php echo TEXT_SEARCH?> ">
	  </form>
	</td>
  </tr>
  <tr>
  	<td width="100" class="font_12px_gray_h18" align="center"><?php echo TEXT_PHOTO?></td>
  	<td width="100" class="font_12px_gray_h18" align="center"><?php echo TEXT_NAME?></td>
    <td width="100" class="font_12px_gray_h18" align="center"><?php echo TEXT_VARIETY?></td>
    <td width="500" class="font_12px_gray_h18" align="center"><?php echo TEXT_PET_INTRO?></td>
    <td width="100" class="font_12px_gray_h18" align="center"><?php echo TEXT_GENDER?></td>
  </tr>
  
    <?php
    $db = new db;
    if(isset($_GET['submit'])) {
    	$where = '';
    	if($_GET['age'] == 'all') {
    		$where .= '';
    	}
    	else {
    		$age_arr = explode("-", $_GET['age']);
    		$where .= ' and age>='.$age_arr[0].' and age<='.$age_arr[1];
    	}
    	
    	if($_GET['sex'] == 'all') {
    		$where .= '';
    	}
    	else {
    		$where .= ' and PGender='.$_GET['sex'];
    	}
    	
    	if($_GET['spay'] == 'all') {
    		$where .= '';
    	}
    	else {
    		$where .= ' and spay_YN="'.$_GET['spay'].'"';
    	}
    	
    	if($_GET['area'] != '' && $_GET['area'] != 'all') {
    		$where .= ' and Rescuer_address like "%'.$_GET['area'].'%"';
    	}
    	
    	$sql = "SELECT PAId, PAPetName, PVariety, PType, PGender, PPhoto_main_thumb ,SUBSTRING(Pet_otherinfo,1,55) AS intro FROM pet_info where PType =2 AND PIsAdopt = 1".$where." order by PAId DESC";
    }
    else {
    	$sql = "SELECT PAId, PAPetName, PVariety, PType, PGender, PPhoto_main_thumb ,SUBSTRING(Pet_otherinfo,1,55) AS intro FROM pet_info where PType =2 AND PIsAdopt = 1 order by PAId DESC";
    }
	$res = $db->query($sql);
	$num = $db->num($res);
	
	if($num > 0) {
		$pagination = new Pagination($num, PAGICATION_RECORD_COUNT_PET, $_GET['page'], PAGINATION_DELTA);
		$sql = $sql." limit ".$pagination->begin.",".$pagination->end;
		$res = $db->query($sql);
		while($row = $db->fetch($res)) {
			
    ?>
    <tr>      
      <td align="center"><a href="RescuePetsInfo.php?Id=<?php echo $row['PAId']; ?>" class="gray_12px"><img src="<?php echo IMAGES_PET_FOLDER.$row['PPhoto_main_thumb']; ?>" border="0" class="img_border"></a></td>
      <td align="center"><a href="RescuePetsInfo.php?Id=<?php echo $row['PAId']; ?>" class="gray_12px"><?php echo $row['PAPetName']; ?></a></td>
      <td align="center"><?php echo $row['PVariety']; ?></td>
      <td align="center"><?php echo $row['intro']; ?>...&nbsp; </td>
      <td align="center"><?php echo getGender($row['PGender']);?> </td>
    </tr>
    <?php
		}
	}
    ?>
</table>
<br />
<table border="0" align="left" cellpadding="0" cellspacing="5" class="font_12px_gray">
            <tr>
              <td>
              <?php
				if($num > PAGICATION_RECORD_COUNT_PET) {
					echo $pagination->paginationString;
				}
				?>
              </td>
              </tr>
          </table></p></td>
    </td>
  </tr>
</table>
</body>
</html>